package javaseof10day.day9.am.demo1;

/*
 * FileName:StuView.class
 * 描述：以特定的方式展示数据
 * @Atuthor:nazi
 * @version:1.0
 * */
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
 
/*
 * StuView 类用于展示数据
 * @author：nazi
 * @version:1.0 
 * */
public class StuView  {	
	private static Cotroller cotroller;
	public static void main(String args[]){
		//创建管理者
		cotroller = new Cotroller(new ArrayList<Student>());
		//界面
		initFrame();
	}
	
	/*
	 * InitFrame()中含有各种类型的控件，以及控件所对应的事件处理步骤
	 * */
	protected static void initFrame(){
			JFrame frame = new JFrame("学生信息管理系统");
			frame.setSize(600,600);
			frame.setLocation(500, 100);
			frame.setLayout(null);
			//生成组件
			final JTextField name = new JTextField();
			name.setBounds(79, 10, 103, 25);
			final JTextField num = new JTextField();
			num.setBounds(79, 53, 103, 25);
			final JTextField sex = new JTextField();
			sex.setBounds(79, 101, 103, 25);
			final JTextField age = new JTextField();
			age.setBounds(79, 161, 103, 25);
			final JTextField g1 = new JTextField();
			g1.setBounds(79, 216, 103, 25);
 
			final JTextArea show = new JTextArea();
			show.setBounds(194, 12, 388, 274);
			frame.add(show);
			show.setFont(new Font("Serif",Font.BOLD,18));
		
			
			
			frame.add(show);
			frame.add(name);
			frame.add(num);
			frame.add(sex);
			frame.add(age);
			frame.add(g1);
			frame.add(show);
			
			JLabel label = new JLabel("学号：");
			label.setBounds(12, 55, 63, 13);
			frame.getContentPane().add(label);
			
			JLabel label_1 = new JLabel("姓名：");
			label_1.setBounds(12, 10, 63, 13);
			frame.getContentPane().add(label_1);
			
			JLabel label_2 = new JLabel("性别：");
			label_2.setBounds(12, 110, 63, 13);
			frame.getContentPane().add(label_2);
			
			JLabel label_3 = new JLabel("年龄：");
			label_3.setBounds(12, 167, 63, 13);
			frame.getContentPane().add(label_3);
			
			JLabel label_4 = new JLabel("成绩：");
			label_4.setBounds(12, 226, 70, 13);
			frame.getContentPane().add(label_4);
			
			
			
			//添加学生
			JButton btnAdd =new JButton("添加");
			btnAdd.setBounds(12, 362, 104, 23);
			frame.add(btnAdd);
			btnAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Student s1 = new Student(Integer.parseInt(num.getText()),name.getText(), sex.getText(),Integer.parseInt(age.getText()),Integer.parseInt(g1.getText()));
					//放到集合
					cotroller.getList().add(s1);
					//打印
					for(int i = 0;i<cotroller.getList().size();i++){
						show.append("\n");
						show.append(cotroller.printAll(i));
					}
					
					
				}
			});
			
			//保存为文件
			JButton btnSave =new JButton("保存");;
			btnSave.setBounds(478, 362, 104, 23);
			frame.add(btnSave);
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				
						try {
							cotroller.fileOt();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			
				}
			});
			
			//刷新
			JButton  btnRefresh = new JButton("刷新");
			btnRefresh.setBounds(327, 362, 104, 23);
			frame.add(btnRefresh);
			btnRefresh.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					try {
						cotroller.fileIn();
						
						
						//打印
						for(int i = 0;i<cotroller.getList().size();i++){
							show.append("\n");
							show.append(cotroller.printAll(i));
						}
						
						
						
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			
					
				}
			});
			
			//删除
			JButton button_1 = new JButton("删除");
			button_1.setBounds(169, 362, 104, 23);
			button_1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			frame.add(button_1);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);	
		}
 
}

